Conditions | 8 |
Paths | 160 |
Total Lines | 75 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | wp.customize.controlConstructor['kirki-number'] = wp.customize.kirkiDynamicControl.extend({ |
||
3 | initKirkiControl: function() { |
||
4 | |||
5 | var control = this, |
||
|
|||
6 | value = control.setting._value, |
||
7 | html = '', |
||
8 | input, |
||
9 | up, |
||
10 | down; |
||
11 | |||
12 | // Make sure we use default values if none are define for some arguments. |
||
13 | control.params.choices = _.defaults( control.params.choices, { |
||
14 | min: 0, |
||
15 | max: 100, |
||
16 | step: 1 |
||
17 | } ); |
||
18 | |||
19 | // Make sure we have a valid value. |
||
20 | if ( isNaN( value ) || '' === value ) { |
||
21 | value = ( 0 > control.params.choices.min && 0 < control.params.choices.max ) ? 0 : control.params.choices.min; |
||
22 | } |
||
23 | value = parseFloat( value ); |
||
24 | |||
25 | // If step is 'any', set to 0.001. |
||
26 | control.params.choices.step = ( 'any' === control.params.choices.step ) ? 0.001 : control.params.choices.step; |
||
27 | |||
28 | // Make sure choices are properly formtted as numbers. |
||
29 | control.params.choices.min = parseFloat( control.params.choices.min ); |
||
30 | control.params.choices.max = parseFloat( control.params.choices.max ); |
||
31 | control.params.choices.step = parseFloat( control.params.choices.step ); |
||
32 | |||
33 | // Build the HTML for the control. |
||
34 | html += '<label>'; |
||
35 | if ( control.params.label ) { |
||
36 | html += '<span class="customize-control-title">' + control.params.label + '</span>'; |
||
37 | } |
||
38 | if ( control.params.description ) { |
||
39 | html += '<span class="description customize-control-description">' + control.params.description + '</span>'; |
||
40 | } |
||
41 | html += '<div class="customize-control-content">'; |
||
42 | html += '<input ' + control.params.inputAttrs + ' type="text" ' + control.params.link + ' value="' + value + '" />'; |
||
43 | html += '<div class="quantity button minus">-</div>'; |
||
44 | html += '<div class="quantity button plus">+</div>'; |
||
45 | html += '</div>'; |
||
46 | html += '</label>'; |
||
47 | |||
48 | control.container.html( html ); |
||
49 | |||
50 | input = control.container.find( 'input' ); |
||
51 | up = control.container.find( '.plus' ); |
||
52 | down = control.container.find( '.minus' ); |
||
53 | |||
54 | up.click( function() { |
||
55 | var oldVal = parseFloat( input.val() ), |
||
56 | newVal; |
||
57 | |||
58 | newVal = ( oldVal >= control.params.choices.max ) ? oldVal : oldVal + control.params.choices.step; |
||
59 | |||
60 | input.val( newVal ); |
||
61 | input.trigger( 'change' ); |
||
62 | } ); |
||
63 | |||
64 | down.click( function() { |
||
65 | var oldVal = parseFloat( input.val() ), |
||
66 | newVal; |
||
67 | |||
68 | newVal = ( oldVal <= control.params.choices.min ) ? oldVal : oldVal - control.params.choices.step; |
||
69 | |||
70 | input.val( newVal ); |
||
71 | input.trigger( 'change' ); |
||
72 | } ); |
||
73 | |||
74 | this.container.on( 'change keyup paste click', 'input', function() { |
||
75 | control.setting.set( jQuery( this ).val() ); |
||
76 | }); |
||
77 | } |
||
78 | }); |
||
79 |
Since ECMAScript 6, you can create block-scoped vars or constants with the keywords
let
orconst
. These variables/constants are only valid in the code block where they have been declared.Consider the following two pieces of code:
and
The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.
To know more about this ECMA6 feature, look at the MDN pages on let and const.